package com.che.libcommon.ui.loading;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.view.View;
import com.che.libcommon.ui.BaseFragment;
import com.che.libcommon.ui.VOMessage;

/**
 * Created by zl on 2017/12/21.
 */

public abstract class LoadingFragment extends BaseFragment implements ILoadingView {


  private LoadingDelegate mLoadingDelegate;

  @Override
  public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    if(needLoading()) {
      View rootView = view;
      if(getLoadingRootId() != 0){
        rootView = view.findViewById(getLoadingRootId());
      }
      mLoadingDelegate = new LoadingDelegate(rootView, getLoadingFrameId(),getConfigure());

    }
  }

  @Override
  public void showLoading(boolean isShow) {
    if(mLoadingDelegate != null){
      mLoadingDelegate.showLoading(isShow);
    }
  }

  @Override
  public void showError(VOMessage voError) {
    if(mLoadingDelegate != null){
      mLoadingDelegate.showError(voError);
    }
  }

  @Override
  public void showData() {
    if(mLoadingDelegate != null){
      mLoadingDelegate.showData();
    }
  }

  public void setTextMsgStyle(@StyleRes int tvMsgTitleResId,@StyleRes int tvMsgToResId){
    if(mLoadingDelegate != null){
      mLoadingDelegate.setTextMsgStyle(tvMsgTitleResId, tvMsgToResId);
    }
  }

  protected void showError(Throwable throwable){
    showError(VOMessage.create(throwable));
  }

  protected abstract @IdRes int getLoadingFrameId();

  protected @IdRes int getLoadingRootId(){
    return 0;
  }

  protected boolean needLoading(){
    return true;
  }

}
